package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 会员SERVICE实现类
 */
@Service
@Slf4j
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查看会员是否存在
     * @param validateCode
     * @param telephone
     * @return
     */
    @Override
    public Boolean findByTelephone(String validateCode, String telephone) {
        // 根据手机号去查询会员表中是否存在此会员
        Member member = memberDao.findByTelephone(telephone);
        // 如果为空，说明没有此会员，需要新增会员
        if(member == null){
            member = new Member();
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            member.setName(telephone);
            memberDao.insert(member);
        }
        // 获取到redis中的验证码
        String code = redisTemplate.opsForValue().get("telephone")+"";
//        log.info("登录用户的手机号验证码: {}", code);
        // 判断验证码是否正确
        if(validateCode.equals(code)){
            return true;
        }
        return false;
    }
}
